#include "imimp4_ffmpeg_api_impl.h"

#ifdef WIN32
#define LOGE
#else
#include "log.h"
#endif

using namespace imimp4_namespace;

imimp4ffmpeg_impl::imimp4ffmpeg_impl():
_mp4_handle(0)
{

}

imimp4ffmpeg_impl::~imimp4ffmpeg_impl()
{

}

int imimp4ffmpeg_impl::create(const char* path,
							  container_format_id container,
							  video_codec_id video,
							  audio_codec_id audio,
							  unsigned int vfps,
							  unsigned int vwidth,
							  unsigned int vheight,
							  unsigned int achannel,
							  unsigned int asamplerate)
{
	LOGE("imimp4_create_file begin imimp4ffmpeg_impl path = %s container = %d video = %d audio = %d vfps = %d vwidth = %d vheight = %d achannel = %d asamplerate = %d\n",
		path, container, video, audio, vfps, vwidth, vheight, achannel, asamplerate);
	void* handle = 0;
	int r = imimp4_create_file(path, container, video, audio, vfps, vwidth, vheight, achannel, asamplerate, &handle);
	if (r == 0)
	{
		_mp4_handle = handle;
	}
	LOGE("imimp4_create_file end imimp4ffmpeg_impl r = %d\n", r);
	return r;
}

int imimp4ffmpeg_impl::write_video(unsigned char* data,
								   unsigned int data_len,
								   unsigned int vtimestamp,
								   bool iskeyframe)
{
	LOGE("imimp4_write_video_frame begin imimp4ffmpeg_impl data = 0x%x data_len = %d vtimestamp = %d iskeyframe = %d\n",
		data, data_len, vtimestamp, iskeyframe);
	return imimp4_write_video_frame(_mp4_handle, data, data_len, vtimestamp, iskeyframe);
}

int imimp4ffmpeg_impl::write_audio(unsigned char* data,
								   unsigned int data_len,
								   unsigned int atimestamp)
{
	LOGE("imimp4_write_audio_frame begin imimp4ffmpeg_impl data = 0x%x data_len = %d atimestamp = %d\n",
		data, data_len, atimestamp);
	return imimp4_write_audio_frame(_mp4_handle, data, data_len, atimestamp);
}

int imimp4ffmpeg_impl::close_create()
{
	LOGE("imimp4_close_file_for_create begin imimp4ffmpeg_impl\n");
	return imimp4_close_file_for_create(_mp4_handle);
}

int imimp4ffmpeg_impl::open(const char* path,
							container_format_id* container,
							video_codec_id* video,
							audio_codec_id* audio,
							unsigned int* vfps,
							unsigned int* vwidth,
							unsigned int* vheight,
							unsigned int* achannel,
							unsigned int* asamplerate,
							unsigned int* abitrate)
{
	LOGE("imimp4_open_file begin imimp4ffmpeg_impl path = %s\n", path);
	void* handle = 0;
	int r = imimp4_open_file(path, container, video, audio, vfps, vwidth, vheight, achannel, asamplerate, abitrate, &handle);
	if (r == 0)
	{
		_mp4_handle = handle;
	}
	LOGE("imimp4_open_file end imimp4ffmpeg_impl path = %s container = %d video = %d audio = %d vfps = %d vwidth = %d vheight = %d achannel = %d asamplerate = %d abitrate = %d\n",
		path, *container, *video, *audio, *vfps, *vwidth, *vheight, *achannel, *asamplerate, *abitrate);
	return r;
}

int imimp4ffmpeg_impl::get_frame(unsigned char* data,
								 unsigned int* data_len,
								 unsigned int* timestamp,
								 bool* iskeyframe,
								 bool* isaudioframe)
{
	LOGE("imimp4_get_video_frame begin imimp4ffmpeg_impl data = 0x%x\n", data);
	return imimp4_get_video_frame(_mp4_handle, data, data_len, timestamp, iskeyframe, isaudioframe);
}

int imimp4ffmpeg_impl::close_open()
{
	LOGE("imimp4_close_file_for_open begin imimp4ffmpeg_impl\n");
	return imimp4_close_file_for_open(_mp4_handle);
}